<template>
  <div class="base-container">
    <t-form
      ref="form"
      :data="queryParams"
      label-width="80"
      @submit="
        () => {
          this.pagination.current = 1;
          this.queryParams.pageNum = 1;
          this.getList();
        }
      "
      @reset="
        () => {
          this.pagination.current = 1;
          this.queryParams.pageNum = 1;
          this.getList();
        }
      "
    >
      <t-row style="border-bottom: 1px solid #e5e6eb; margin-bottom: 10px">
        <t-col :span="11" class="query-form-inline">
          <!-- <t-form-item label="成果名称" name="name">
            <t-input v-model="queryParams.name" clearable placeholder="请输入成果名称" style="width: 240px" />
          </t-form-item> -->
          <!-- <t-form-item label="成果类别" name="type">
            <t-select
              style="width: 240px"
              clearable
              v-model="queryParams.type"
              :options="dict.type.cgzh_ministry_result_type1"
            >
            </t-select>
          </t-form-item> -->
          <t-form-item label="单位名称" name="name">
            <t-input v-model="queryParams.name" clearable placeholder="请输入单位名称" style="width: 240px" />
          </t-form-item>
          <!-- <t-form-item label="联系人" name="linkman">
            <t-input v-model="queryParams.linkman" clearable placeholder="请输入联系人" style="width: 240px" />
          </t-form-item> -->
        </t-col>
        <t-col :span="1" class="operation-container">
          <t-button theme="primary" type="submit">查询</t-button>
          <t-button theme="default" variant="outline" type="reset">重置</t-button>
        </t-col>
      </t-row>
    </t-form>
    <div class="btn-group-inline">
      <t-button @click="dialog.header='新增调查单位'; dialog.open_add=true; dialog.isUpdate=false; currentRow={}" variant="outline" theme="primary">
        <t-icon name="add" slot="icon"/>
        添加
      </t-button>
    </div>
      <t-table rowKey="id" :data="data" :columns="columns" hover :pagination="pagination" :loading="dataLoading">
      <template #name="{ row }">
        <t-link
          @click="
            dialog.objId = row.id;
            dialog.header = row.name;
            currentRow=row;
            dialog.open_detail = true;
          "
          hover="color"
          theme="primary"
          >{{ row.name }}</t-link
        >
      </template>
      <template #manageOrgCode="{ row }">
        {{ row.manageOrgCode | filterByDict(dict.type.kpzh_manage_org_code) }}
      </template>
      <template #nationalEconomyCode="{ row }">
        {{ row.nationalEconomyCode | filterByDict(dict.type.kpzh_national_economy_code) }}
      </template>
      <template #unitLevel="{ row }">
        {{ row.unitLevel | filterByDict(dict.type.kpzh_unit_level) }}
      </template>
      <template #sourceFunds="{ row }">
        {{ row.sourceFunds | filterByDict(dict.type.kpzh_source_funds) }}
      </template>
      <template #op="{ row }">
        <t-button @click="dialog.header='编辑调查单位'; currentRow=row; dialog.open_add=true; dialog.isUpdate=true" size="small" variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon" />
          编辑
        </t-button>
        <t-button  theme="danger" @click="delObj(row)" size="small" variant="text" class="t-button-link">
          <t-icon name="edit-1" slot="icon" />
          删除
        </t-button>
        <t-button
          size="small"
          @click="
            dialog.objId = row.id;
            dialog.header = row.name;
            currentRow=row;
            dialog.open_detail = true;
          "
          variant="text"
          class="t-button-link"
        >
          <t-icon name="info-circle" slot="icon" />
          详情
        </t-button>
      </template>
    </t-table>

    <unit-add-edit-dialog
      :objId="dialog.objId"
      :header="dialog.header"
      :row="currentRow"
      :isUpdate="dialog.isUpdate"
      :visible.sync="dialog.open_add"
      v-if="dialog.open_add"
      @reload="getList"
    >
    </unit-add-edit-dialog>
    <unit-detail-dialog 
      :objId="dialog.objId"
      :header="dialog.header"
      :row="currentRow"
      :visible.sync="dialog.open_detail"
      v-if="dialog.open_detail"></unit-detail-dialog>
  </div>
</template>
  
<script>

import unitAddEditDialog from './components/unit-add-edit-dialog.vue'
import unitDetailDialog from './components/unit-detail-dialog.vue'

export default {
  name: 'extend-index',
  dicts: ['kpzh_manage_org_code', 'kpzh_national_economy_code', 'kpzh_unit_level', 'kpzh_source_funds'],
  components: {
    unitAddEditDialog,
    unitDetailDialog
  },
  data() {
    return {
      dialog: {
        header: '',
        open: false,
        objId: null,
        open_detail: false,
        open_add: false,
        isUpdate: false
      },
      queryParams: {
        pageNum: 1,
        pageSize: 10
      },
      data: [],
      currentRow: {},
      columns: [
        {
          colKey: 'code',
          title: '统一社会信用代码',
          align: 'center',
        },
        {
          colKey: 'name',
          title: '单位详细名称',
          align: 'center',
        },
        {
          colKey: 'orgStats',
          title: '机构属性',
          align: 'center',
        },
        // {
        //   colKey: 'orgStatsName',
        //   title: '机构属性名称',
        //   align: 'center',
        // },
        {
          colKey: 'unitLevel',
          title: '单位级别',
          align: 'center',
        },
        {
          colKey: 'nationalEconomyCode',
          title: '所属国民经济行业分类门类代码',
          align: 'center',
        },
        {
          colKey: 'sourceFunds',
          title: '单位经费来源情况',
          align: 'center',
        },
        {
          colKey: 'manageOrgCode',
          title: '机构主管部门类别代码',
          align: 'center',
        },
        {
          colKey: 'op',
          title: '操作',
          align: 'center',
          width: 140,
          fixed: 'right',
        },
      ],
      dataLoading: false,
      pagination: {
        current: 1,
        pageSize: 10,
        total: 0,
        showPageSize: true,
        pageSizeOptions: [10, 20, 30, 50],
        showFirstAndLastPageBtn: false,
        onChange: (pageInfo) => {
          Object.assign(this.pagination, pageInfo);
          this.queryParams.pageNum = pageInfo.current || 1;
          this.queryParams.pageSize = pageInfo.pageSize;
          this.getList();
        },
      },
    };
  },
  created() {
    Object.assign(this.queryParams, this.handelQuery(this.queryParams, this.$route.query));
    this.getList();
  },
  methods: {
    getList() {
      this.dataLoading = true;
      this.$api.kpzh.unit.myList(this.queryParams)
        .then((response) => {
          this.data = response.rows;
          this.pagination.total = response.total;
          this.dataLoading = false;
        })
        .catch((e) => {
          this.dataLoading = false;
          this.$message.error(e.toString());
        });
    },
    delObj(row) {
      const confirmDia = this.$dialog.confirm({
        header: "系统提示",
        body: `是否确认删除成果名称为"${row.name}"的数据项？`,
        theme: "warning",
        onConfirm: ({e}) => {
          this.$api.kpzh.unit.delete(row.id).then(res => {
            this.getList();
            this.$message.success("删除成功");
            // 请求成功后，销毁弹框
            confirmDia.destroy();
          }
          ).catch((e) => {
            this.$message.error(e.toString());
          });
        }
      });
    }
  },
};
</script>
  
  <style scoped>
</style>
  